import { defineConfig } from 'vite'
import prismjs from 'vite-plugin-prismjs'
import vue from '@vitejs/plugin-vue'
const path = require('path')

// https://vitejs.dev/config/
export default defineConfig({
    // markdown代码块支持所有语言
    plugins: [
        vue(),
        prismjs({
            languages: 'all'
        })
    ],
    base: './',
    resolve: {
        alias: {
            // 如果报错__dirname找不到，需要安装node,执行npm install @types/node --save-dev
            '@': path.resolve(__dirname, 'src'),
            '@api': path.resolve(__dirname, 'src/api'),
            '@assets': path.resolve(__dirname, 'src/assets'),
            '@components': path.resolve(__dirname, 'src/components'),
            '@images': path.resolve(__dirname, 'src/assets/images'),
            '@views': path.resolve(__dirname, 'src/views'),
            '@store': path.resolve(__dirname, 'src/store')
        }
    },
    build: {
        outDir: 'dist',
        // 依赖包切割，处理打包文件打过的情况
        rollupOptions: {
            output: {
                manualChunks(id) {
                    if (id.includes('node_modules')) {
                        return id.toString().split('node_modules/')[1].split('/')[0].toString()
                    }
                }
            }
        }
    },
    server: {
        https: false, // 是否开启 https
        open: true, // 是否自动在浏览器打开
        port: 8080, // 端口号
        host: '0.0.0.0', // Network: use `--host` to expose
        proxy: {
            // 本地开发环境通过代理实现跨域，生产环境使用 nginx 转发
            // 正则表达式写法
            '^/api': {
                target: 'http://192.168.0.168:3000/', // 后端服务实际地址
                changeOrigin: true, //开启代理
                rewrite: path => path.replace(/^\/api/, '')
            }
        }
    },
    // 引入第三方的配置
    optimizeDeps: {
        include: []
    }
})
